
#include "e_mod_main.h"
#ifdef HAVE_LIBMPD_LIBMPDCLIENT_H
#include <libmpd/libmpdclient.h>


static int _mpd_connect(Eloquence_Face *face);

static void _mpd_control_vol(Eloquence_Face *face, int value);

void mpd_init_menu(E_Menu *menu, Eloquence_Face *face){
  E_Menu_Item *mi;
  
  mi = e_menu_item_new(menu);
  e_menu_item_label_set(mi, _("Play/Pause"));
  e_menu_item_callback_set(mi, mpd_control_play_pause, face);
  mi = e_menu_item_new(menu);
  e_menu_item_label_set(mi, _("Next Song"));
  e_menu_item_callback_set(mi, mpd_control_track_next, face);
  mi = e_menu_item_new(menu);
  e_menu_item_label_set(mi, _("Previous Song"));
  e_menu_item_callback_set(mi, mpd_control_track_prev, face);
  mi = e_menu_item_new(menu);
  e_menu_item_label_set(mi, _("Shuffle On/Off"));
  e_menu_item_callback_set(mi, mpd_control_shuffle, face);
}

static const char *_mpd_get_text(mpd_Connection *mpd, char *buffer);
static char *_mpd_get_textVol(mpd_Connection *mpd, char *buffer, int volume);


void mpd_connect(Eloquence_Face *face)
{
	if ( _mpd_connect(face) != 0 )
		face->recheck_timer = ecore_timer_add(SEARCH_REFRESH, _mpd_connect, face);
}
void mpd_disconnect(Eloquence_Face *face)
{
	if (face->session.ptr)
	{
		mpd_closeConnection(face->session.ptr);
		face->session.ptr = NULL;
	}

	if (face->recheck_timer)
	{
		ecore_timer_del(face->recheck_timer);
		face->recheck_timer = NULL;
	}
}

int mpd_update_song(Eloquence_Face *face)
{
	mpd_Connection *mpd = face->session.ptr;

	if (mpd)
	{
		mpd_sendStatusCommand(mpd);

		if (mpd->error == 0)
		{
			face->song = _mpd_get_text(mpd, face->outbuf);
			draw_text(face);
		}
		else
		{
			mpd_disconnect(face);
			mpd_connect(face);
		}
	}

	return 1;
}

void mpd_control_play_pause(Eloquence_Face *face){
	mpd_sendStatusCommand(face->session.ptr);
	mpd_Status *status = mpd_getStatus(face->session.ptr);

	if (status)
	{
		if (status->state <= MPD_STATUS_STATE_STOP)
			mpd_sendPlayCommand(face->session.ptr, -1); // -1 == beginning
		else
			mpd_sendPauseCommand(face->session.ptr, status->state == MPD_STATUS_STATE_PLAY);

		mpd_freeStatus(status);
	}
}

void mpd_control_track_next(Eloquence_Face *face){
	mpd_sendNextCommand(face->session.ptr);
	mpd_update_song(face);
}

void mpd_control_track_prev(Eloquence_Face *face){
	mpd_sendPrevCommand(face->session.ptr);
	mpd_update_song(face);
}

void mpd_control_vol_inc(Eloquence_Face *face){
	_mpd_control_vol(face, 2);
}

void mpd_control_vol_dec(Eloquence_Face *face){
	_mpd_control_vol(face, -2);
}

void mpd_control_shuffle(Eloquence_Face *face){
	mpd_sendShuffleCommand(face->session.ptr);
}

void mpd_add_file_callback(Eloquence_Face *face, 
		Evas_Object *obj, const char *emission, const char *source){
	/* Empty now, for drag'n'drop of files to play */
}


static int _mpd_connect(Eloquence_Face *face)
{
	mpd_Connection *mpd = mpd_newConnection("localhost", 6600, 3.0);
	face->session.ptr = mpd;

	// return false on success to stop looping the recheck timer
	return mpd == NULL;
}

static const char *_mpd_get_text(mpd_Connection *mpd, char *buffer)
{
	buffer[0] = '\0';

	mpd_Status *status = mpd_getStatus(mpd);

	if (status)
	{
		if (status->state > MPD_STATUS_STATE_STOP)
		{
			mpd_sendCurrentSongCommand(mpd);
			mpd_InfoEntity *entity = NULL;

			while ( (entity = mpd_getNextInfoEntity(mpd)) )
			{
				if ( entity->type == MPD_INFO_ENTITY_TYPE_SONG &&
					entity->info.song->id == status->songid )
				{
					mpd_Song *song = entity->info.song;

					if (song->artist)
						strcpy(buffer, song->artist);
					if (song->title)
					{
						if (buffer[0] != '\0')
							strcat(buffer, " - ");
						strcat(buffer, song->title);
					}

					// no artist or title, use filename
					if (buffer[0] == '\0')
						strcpy(buffer, song->file);

					if (status->state == MPD_STATUS_STATE_PAUSE)
						strcat(buffer, " (paused)");
				}

				mpd_freeInfoEntity(entity);
			}
		}

		mpd_freeStatus(status);
	}

	return buffer;
}
static char *_mpd_get_textVol(mpd_Connection *mpd, char *buffer, int volume)
{
	sprintf(buffer, "%s	 --	 Volume: %i%%", _mpd_get_text(mpd, buffer), volume);
	return buffer;
}

static void _mpd_control_vol(Eloquence_Face *face, int value)
{
	mpd_sendStatusCommand(face->session.ptr);
	mpd_Status *status = mpd_getStatus(face->session.ptr);

	if (status)
	{
		if (status->volume != MPD_STATUS_NO_VOLUME)
		{
			int volume = status->volume + value;	

			if (volume < 0)
				volume = 0;
			else if (volume > 100)
				volume = 100;

			face->song = _mpd_get_textVol(face->session.ptr, face->outbuf, volume);
			draw_text(face);

			mpd_sendSetvolCommand(face->session.ptr, volume);
		}

		mpd_freeStatus(status);
	}
}


#endif // HAVE_LIBMPD_LIBMPDCLIENT_H
